﻿using System;
using System.Collections.Generic;
using System.Linq;
using ESoft.Core.Data.Service;
using ESoft.Core.Utils.Extensions;
using ESoft.Core.Web.Utils;
using ESoft.Core.Web.VO;
using ESoft.Portal.Areas.ContentManager.Data.Service;
using ESoft.Portal.Data.Domain.Entity;
using ESoft.Portal.Utils;
using Microsoft.AspNetCore.Mvc;

namespace ESoft.Portal.Areas.ContentManager.ApiControllers
{
    [Route("api/content/channel")]
    [ApiController]
    public class ChannelController : ControllerBase
    {
        private readonly IBaseService _baseService;
        private readonly IChannelService _channelService;

        public ChannelController(IBaseService baseService, IChannelService channelService)
        {
            _baseService = baseService;
            _channelService = channelService;
        }

        [Route("page")]
        [HttpGet]
        public object GetPageList([FromQuery]PageInput pageInput, [FromQuery]string keyword, [FromQuery]int? channelType)
        {
            User user = EnvUtils.GetCurrentUser(Request.HttpContext);
            List<Dictionary<string, object>> channelList
                = _baseService.QueryPageEntities<CContentChannelEntity>(pageInput, x => x.OrgId == user.ShopId && (keyword.IsNullOrEmpty() || x.ChannelName.Contains(keyword))&&(channelType==null||x.ChannelType==channelType), new SortVo { SortField = "Sort", SortWay = true })
                    .Select(x => MapUtils.ObjectToMap(x)).ToList();

            foreach (var filterChannel in channelList)
            {
                if (filterChannel[nameof(CContentChannelEntity.FatherChannelId)].IsNotNull() &&
                    filterChannel[nameof(CContentChannelEntity.FatherChannelId)].ToString().IsNotNullOrWhiteSpace())
                {
                    var cc = _baseService.Find<CContentChannelEntity>(filterChannel[nameof(CContentChannelEntity.FatherChannelId)]);
                    if (cc != null)
                        filterChannel["FatherChannelName"] = cc.ChannelName;
                }
                else
                {
                    filterChannel["FatherChannelName"] = null;
                }
            }

            var count = _baseService.Count<CContentChannelEntity>(x => x.OrgId == user.ShopId && (keyword.IsNullOrEmpty() || x.ChannelName.Contains(keyword)) && (channelType == null || x.ChannelType == channelType));

            return ResponseResult.Success(new PageResult<dynamic>(channelList, count));
        }

        [Route("Query")]
        [HttpGet]
        public object Query()
        {
            User user = EnvUtils.GetCurrentUser(Request.HttpContext);
            var channelList = _baseService.QueryEntities<CContentChannelEntity>(x => x.OrgId == user.ShopId);
            return ResponseResult.Success(channelList);
        }

        [HttpDelete]
        public object Delete([FromBody] dynamic dy)
        {
            string channelId = dy.channelId;
            User user = EnvUtils.GetCurrentUser(Request.HttpContext);
            _channelService.Delete(channelId);
            return ResponseResult.Success();
        }

       

        [Route("{channelId}")]
        [HttpGet]
        public object Get([FromRoute]string channelId)
        {
            User user = EnvUtils.GetCurrentUser(Request.HttpContext);
            var channel = _baseService.Find<CContentChannelEntity>(channelId);
            var richText = _baseService.Find<CRichTextEntity>(channel.RichTextId);
            var result = new Dictionary<String, object>
            {
                {"channelId",channel.ChannelId },
                {"channelName",channel.ChannelName },
                {"channelType",channel.ChannelType },
                {"createDatetime",channel.CreateDatetime },
                {"fatherChannelId",channel.FatherChannelId },
                {"imageId",channel.ImageId },
                {"isHot",channel.IsHot },
                {"sort",channel.Sort },
                {"url",channel.Url },
                {"fullDetail", System.Text.Encoding.Default.GetString(richText.TextContent) },
            };

            return ResponseResult.Success(result);
        }

        [Route("addChannel")]
        [HttpPost]
        public object AddChannel([FromBody]dynamic input)
        {
            User user = EnvUtils.GetCurrentUser(Request.HttpContext);
            _channelService.Add(input, user);
            return ResponseResult.Success();
        }

       

        [Route("updateChannel")]
        [HttpPut]
        public object UpdateChannel([FromBody]dynamic input, [FromQuery]string channelId)
        {
            User user = EnvUtils.GetCurrentUser(Request.HttpContext);
           _channelService.Update(input, channelId, user);
            return ResponseResult.Success();
        }
    }
}